package cn.consult.sys.service.impl;

import cn.consult.sys.dao.TEnterpriseDao;
import cn.consult.sys.dao.TPlaceBuildingDao;
import cn.consult.sys.dao.TPlaceFloorDao;
import cn.consult.sys.dao.TPlaceRoomDao;
import cn.consult.sys.entity.TEnterprise;
import cn.consult.sys.entity.TPlaceBuilding;
import cn.consult.sys.entity.TPlaceFloor;
import cn.consult.sys.entity.TPlaceRoom;
import cn.consult.sys.service.TEnterpriseService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * (TEnterprise)表服务实现类
 *
 * @author makejava
 * @since 2024-04-08 09:11:34
 */
@Service("tEnterpriseService")
public class TEnterpriseServiceImpl extends ServiceImpl<TEnterpriseDao, TEnterprise> implements TEnterpriseService {

    @Autowired
    private TEnterpriseDao tEnterpriseDao;
    @Autowired
    private TPlaceBuildingDao tPlaceBuildingDao;
    @Autowired
    private TPlaceFloorDao tPlaceFloorDao;
    @Autowired
    private TPlaceRoomDao tPlaceRoomDao;

    @Override
    public IPage<TEnterprise> getEmployeeAll(Map<String, Object> params) {
        LambdaQueryWrapper<TEnterprise> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(params.get("enterpriseName") != null && params.get("enterpriseName") != "", TEnterprise::getEnterpriseName, params.get("enterpriseName"));
        queryWrapper.like(params.get("enterprisePrincipal") != null && params.get("enterprisePrincipal") != "", TEnterprise::getEnterprisePrincipal, params.get("enterprisePrincipal"));
        queryWrapper.like(params.get("address") != null && params.get("address") != "", TEnterprise::getAddress, params.get("address"));

        Page page = new Page();
        page.setCurrent(Integer.parseInt(params.get("pageNum").toString()));
        page.setSize(Integer.parseInt(params.get("pageSize").toString()));

        Page page1 = tEnterpriseDao.selectPage(page, queryWrapper);


        return page1;
    }

    @Override
    public boolean saveEnterprise(TEnterprise tEnterprise) {
        String address = null;
        if (tEnterprise.getBulidingId() != null) {
            TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(tEnterprise.getBulidingId());
            address = tPlaceBuilding.getBuildingName()+"-";
        }
        if (tEnterprise.getFloorId() != null) {
            TPlaceFloor tPlaceFloor = tPlaceFloorDao.selectById(tEnterprise.getFloorId());
            address = address + tPlaceFloor.getFloorName()+"-";
        }
        if (tEnterprise.getRoomId() != null) {
            TPlaceRoom tPlaceRoom = tPlaceRoomDao.selectById(tEnterprise.getRoomId());
            address = address + tPlaceRoom.getRoomName();
        }
        tEnterprise.setAddress(address);
        int insert = tEnterpriseDao.insert(tEnterprise);


        return insert > 0 ? true : false;
    }

    @Override
    public boolean updateEnterprise(TEnterprise tEnterprise) {
        String address = null;
        if (tEnterprise.getBulidingId() != null) {
            TPlaceBuilding tPlaceBuilding = tPlaceBuildingDao.selectById(tEnterprise.getBulidingId());
            address = tPlaceBuilding.getBuildingName()+"-";
        }
        if (tEnterprise.getFloorId() != null) {
            TPlaceFloor tPlaceFloor = tPlaceFloorDao.selectById(tEnterprise.getFloorId());
            address = address + tPlaceFloor.getFloorName()+"-";
        }
        if (tEnterprise.getRoomId() != null) {
            TPlaceRoom tPlaceRoom = tPlaceRoomDao.selectById(tEnterprise.getRoomId());
            address = address + tPlaceRoom.getRoomName();
        }
        tEnterprise.setAddress(address);
        int update = tEnterpriseDao.updateById(tEnterprise);


        return update > 0 ? true : false;
    }
}

